package com.xaptul.docpreview.action;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.xaptul.docpreview.util.XaptulLog;

@Controller
public class ExceptionController {

	private Logger log = XaptulLog.getLogger();

	/** 抛出的异常要记录
	 * @param e 
	 * @param req
	 * @return 
	 * @throws IOException
	 */
	@ExceptionHandler(java.lang.Exception.class)
	public String handleException(java.lang.Exception e, HttpServletRequest req)
			throws IOException {
		log.error(e.getMessage() + " \nrequest url : " + req.getRequestURL(), e);
		return "redirect:/docpreview/index.html";
	}

	/**
	 * @param model
	 * @return 访问了主页就重定向到上传页面
	 */
	@RequestMapping(value = "/")
	public String main(Model model) {
		return "redirect:/docpreview/index.html";
	}

	/**
	 * @param request
	 * @return 404 错误都返回到默认页面
	 */
	@RequestMapping(value = "/exception/resourceNotFound.html", method = RequestMethod.GET)
	public String resourceNotFound(HttpServletRequest request) {
		return "redirect:/docpreview/index.html";
	}
}
